package main

import (
	"gitee.com/goxiang2/server/lib/lib_rpc"
	"github.com/golang/protobuf/proto"
	"log"
	"net"
	"time"
)

type User struct{}

func (t *User) Add(args *AddReq, reply *AddRsp) (uint32, error) {
	reply.C = args.A + args.B
	return 123, nil
}

func main() {
	s := lib_rpc.NewServer()

	s.RegisterName("AddServer", &User{})

	l, err := net.Listen("tcp", ":12323")
	if err != nil {
		log.Fatal("listen error:", err)
	}
	go s.Accept(l)

	// var rsp AddRsp
	// ret, _ := s.InternalCall("AddServer.Add", &AddReq{123123, 3}, &rsp)
	// log.Println(ret, rsp.C)

	// req := &AddReq{123123, 3}
	// // var rsp AddRsp
	// var buff1 []byte
	// buff, _ := proto.Marshal(req)
	// ret, buff1, _ = s.RawCall("AddServer.Add", buff)
	// proto.Unmarshal(buff1, &rsp)
	// log.Println(ret, rsp.C)

	c, _ := lib_rpc.Dial("tcp", "127.0.0.1:12323")

	req := &AddReq{123123, 3}
	var rsp AddRsp
	buff, _ := proto.Marshal(req)
	ret, buff1, err1 := c.RawCall("AddServer.Add", buff)
	log.Println(ret, err1)
	proto.Unmarshal(buff1, &rsp)
	log.Println(rsp.C)
	time.Sleep(100 * time.Second)
}
